/*
 * StatusCode.c
 *
 *  Created on: Jan 16, 2010
 *      Author:
 */

#include "StatusCode.h"

Status_Msg key_Msg[] = {
	{HMC_Success,"success!\n"},
	{HMC_Unknown_Equipment,"unkown equipment!\n"},
	{HMC_Unknown_Attribute,"unkown attribute!\n"},
	{HMC_Unknown_Attribute_Type,"unknown attribute type!\n"},
	{HMC_Unknown_Attribute_CurrentValue,"Can't specfic currentVal from attribute!\n"},
	{HMC_Unknown_Attribute_Parameter,"Unknow specfic attribute's \
		parameter for attribute!\n"},
	{HMC_InputValue_Outof_Range,"input value out of range!\n"},
	{HMC_Uknow_Equipment_Operation,"unknow equipment operation!\n"},
	{HMC_Equipment_Operat_Failed,"equipment operat fail!\n"},
	{HMC_Equipment_Operat_Update_Failed,"equipment operat update fail!\n"},
	{HMC_Unknown_Message,"unknown message!\n"},
	{HMC_Unknown_Port,"unknown port!\n"},
	{HMC_Invail_Parameter,"invail parameter!\n"},
	{HMC_Message_ERROR,"message format error!\n"},
	{HMC_PortSend_Error,"send to port error!\n"},
	{HMC_PortGet_Error,"get information form port error!\n"},
	{HMC_PortSet_Error,"port setting error!\n"},
	{HMC_Exist_Equipment,"have exist equipment!\n"},
	{HMC_Port_Occupied,"the port is occupied!\n"},
	{HMC_Open_DataBase_Fail,"can not open database fail!\n"},
	{HMC_Add_Equipment_Error,"add equipment error!\n"},
	{HMC_Open_File_Fail,"open file fail!\n"},
	{HMC_TimeOut,"time out\n"},
};

StatusCode::StatusCode(int state)
{
	status = state;
	status_msg = NULL;
}

StatusCode::~StatusCode()
{
	delete  status_msg;
}

int StatusCode::IfError()
{
	if(status == HMC_Success)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

char* StatusCode::PrintStatus(const string &identifier)
{
	string printstr;
	unsigned int  i;
	unsigned int len = sizeof(key_Msg) / sizeof(Status_Msg );  ;

	for (i = 0;i < len;i++){
		if (key_Msg[i].key == getStatus()){
			printstr = identifier + key_Msg[i].msg;
			break;
		}
	}

	if (i == len){
			printstr = identifier + "unknow error!\n";
	}

	char *str = new char[printstr.size() + 1];
	strcpy(str,printstr.c_str());
	setMsg(str);

	return getMsg();
}

void StatusCode::setMsg(char * msg)
{
	delete status_msg;
	status_msg = msg;
}

char *StatusCode::getMsg()const
{
	return status_msg;
}

void StatusCode::setStatus(int state)
{
	status = state;
}

int StatusCode::getStatus()const
{
	return status;
}

